package ui.activity.wallet.presenter;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import ui.activity.wallet.biz.RewardsBiz;
import ui.activity.wallet.contract.RewardsContract;
import ui.model.RewardsBean;
import ui.model.RewardsComparator;

/**
 * Created by chenjian on 19/2/26
 */

public class RewardsPresenter implements RewardsContract.Presenter {

    RewardsContract.View view;
    RewardsBiz biz;
    List<RewardsBean> list = new ArrayList<>();

    @Inject
    public RewardsPresenter(RewardsContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        this.biz = (RewardsBiz) biz;
    }


    @Override
    public void getListInfo(final boolean isRefresh) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        biz.getListInfo(new BaseBiz.Callback<List<RewardsBean>>() {
            @Override
            public void onSuccess(List<RewardsBean> info) {
                list.addAll(info);
                RewardsComparator sort = new RewardsComparator();
                Collections.sort(list, sort);
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(List<RewardsBean> info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

}
